package com.elitel.frame.base.service.impl;

import com.elitel.common.utils.UUIDUtil;
import com.elitel.frame.base.service.SysWhitelistService;
import com.elitel.frame.main.dao.SysWhitelistMapper;
import com.elitel.frame.main.entity.SysWhitelist;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * cas白名单(SysWhitelist)表服务实现类
 *
 * @author makejava
 * @since 2024-01-02 18:10:10
 */
@Service("sysWhitelistService")
public class SysWhitelistServiceImpl implements SysWhitelistService {
    private static final Logger logger = LoggerFactory.getLogger(SysWhitelistServiceImpl.class);

    @Resource
    private SysWhitelistMapper sysWhitelistDao;


    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SysWhitelist queryById(String id) {
        return this.sysWhitelistDao.queryById(id);
    }

    /**
     * 新增数据
     *
     * @param sysWhitelist 实例对象
     * @return 实例对象
     */
    @Override
    public SysWhitelist insert(SysWhitelist sysWhitelist) {
        this.sysWhitelistDao.insert(sysWhitelist);
        return sysWhitelist;
    }

    /**
     * 修改数据
     *
     * @param sysWhitelist 实例对象
     * @return 实例对象
     */
    @Override
    public SysWhitelist update(SysWhitelist sysWhitelist) {
        this.sysWhitelistDao.update(sysWhitelist);
        return this.queryById(sysWhitelist.getId());
    }



    /**
     * 查询数据
     *
     * @param dto
     * @return
     */
    @Override
    public List<SysWhitelist> queryList(SysWhitelist dto) {
        List<SysWhitelist> sysWhitelists = this.sysWhitelistDao.queryList(dto);
        for (SysWhitelist sysWhitelist : sysWhitelists) {
            // 手机号脱敏
            if (StringUtils.isNotEmpty(sysWhitelist.getPhoneNum())) {
                String num = sysWhitelist.getPhoneNum().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
                sysWhitelist.setPhoneNum(num);
            }

            if (StringUtils.isNotEmpty(sysWhitelist.getVerifyFiledVal())) {
                sysWhitelist.setVerifyFiledVal("*************");
            }


        }
        return sysWhitelists;
    }


    @Override
    public SysWhitelist login(String account, String password) {
        return this.sysWhitelistDao.loginSelect(account, password);
    }

    @Override
    public void updateStatus(SysWhitelist sysWhitelist) {
        // 可以加用户权限
        this.sysWhitelistDao.update(sysWhitelist);
    }

    @Override
    public int save(SysWhitelist sysWhitelist) {

        String id = sysWhitelist.getId();
        SysWhitelist dbData = this.queryById(id);
        // 如果手机号中间为脱敏状态 就不更新
        if (!StringUtils.isEmpty(sysWhitelist.getPhoneNum())) {
            if (sysWhitelist.getPhoneNum().contains("*")) {
                sysWhitelist.setPhoneNum(dbData.getPhoneNum());
            }
            if (sysWhitelist.getVerifyFiledVal().contains("*")) {
                sysWhitelist.setVerifyFiledVal(dbData.getVerifyFiledVal());
            }
        }
        if (StringUtils.isEmpty(sysWhitelist.getVerifyFiledDesc()) || "手机号".equals(sysWhitelist.getVerifyFiledDesc())) {
            sysWhitelist.setPhoneNum(sysWhitelist.getVerifyFiledVal());
        }
        if (dbData != null) {
            this.update(sysWhitelist);
        } else {
            sysWhitelist.setId(UUIDUtil.randomUUID());
            sysWhitelist.setStatus("0");
            sysWhitelist.setCreatedTime(new Date());
            this.insert(sysWhitelist);

        }
        return 1;
    }

    /**
     * @param verifyFieldValue
     * @return
     */
    @Override
    public SysWhitelist selectByVerifyField(String verifyFieldValue) {
        SysWhitelist dto = new SysWhitelist();
        dto.setVerifyFiledVal(verifyFieldValue);
        List<SysWhitelist> sysWhitelists = this.queryList(dto);
        if (sysWhitelists != null && !sysWhitelists.isEmpty()) {
            return sysWhitelists.get(0);
        }
        return null;
    }


    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.sysWhitelistDao.deleteById(id) > 0;
    }
}
